package com.ruyuan.eshop.order.mq.listener;

import com.alibaba.fastjson.JSONObject;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.order.domain.request.CancelOrderAssembleRequest;
import com.ruyuan.eshop.order.exception.OrderBizException;
import com.ruyuan.eshop.order.exception.OrderErrorCodeEnum;
import com.ruyuan.eshop.order.service.OrderAfterSaleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lxg
 * @Description:
 * @date 2022/4/1012:11
 */
@Component
@Slf4j
public class CancelRefundListener implements MessageListenerConcurrently {
    @Autowired
    private OrderAfterSaleService orderAfterSaleService;
    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
       try {
           for (MessageExt messageExt:list){
            String message=new String(messageExt.getBody());
               log.info("CancelRefundConsumer message:{}", message);
               CancelOrderAssembleRequest request = JSONObject.parseObject(message, CancelOrderAssembleRequest.class);
                //  执行 取消订单/超时未支付 取消 前的操作
               JsonResult<Boolean> jsonResult = orderAfterSaleService.processCancelOrder(request);
               if (!jsonResult.getSuccess()){
                   throw new OrderBizException(OrderErrorCodeEnum.CONSUME_MQ_FAILED);
               }
           }
           return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
       }catch (Exception e){
           log.error("consumer error", e);
           return ConsumeConcurrentlyStatus.RECONSUME_LATER;
       }
    }
}
